package memnet.context;

import java.util.HashMap;
import java.util.Map.Entry;

/**
 * Bag of Words context
 * @author Justin
 *
 */
public class BOWContext implements Context {
	
	public static void main(String[] args) {
		BOWContext bc1 = new BOWContext();
		BOWContext bc2 = new BOWContext();
		
		bc1.bag.put("hockey", 0.5);
		bc1.bag.put("league", 1.0);	
		
		bc2.bag.put("hockey", 1.0);
		
		System.out.println(bc2.similarity(bc1));
		
	}
	
	HashMap<String, Double> bag;
	
	public BOWContext(){
		bag = new HashMap<String, Double>();
	}
	
	private double getWordScore(String word){
		return bag.get(word);
	}
	
	/**
	 * Returns a number between 0 and 1 indicating similarity
	 */
	@Override
	public double similarity(Context other) {
		// TODO Auto-generated method stub
		if(!( other instanceof BOWContext)){
			return 0;
		}
		BOWContext bc = (BOWContext) other;
		double score = 0;
		for(Entry<String,Double> e : bag.entrySet()){
			String s = e.getKey();
			double d = e.getValue();
			Double otherscore = bc.getWordScore(s);
			if( otherscore != null){
				score += otherscore*d;
			}
		}
		return score/bag.size();
	}

}
